<template>
  <z-paging ref="paging" v-model="dataList" @query="queryList">
    <template #top>
      <view w-full box-border pa-20rpx>
        <view bg="#fff" w-710rpx h-80rpx rounded-40rpx flex justify-between items-center box-border px-40rpx>
          <input
            v-model="keyword"
            @confirm="handleQuery"
            type="text"
            confirm-type="search"
            w-540rpx
            :placeholder="`搜索${typeObj[type]}......`" />
          <image w-40rpx h-40rpx src="@/pages/company/static/search.png" @click="handleQuery"></image>
        </view>
      </view>
    </template>
    <view w-full px-20rpx box-border>
      <view rounded-20rpx bg-white px-20rpx>
        <view v-for="item in dataList" flex justify-between items-center h-110rpx style="border-bottom: 1px solid #eee">
          <view line-clamp-1 break-all w-520rpx>{{ type === 1 ? item.unitName : item.schoolName }}</view>
          <view
            v-if="item.isJoin == 0"
            bg="#FCC300"
            w-100rpx
            h-50rpx
            rounded-20rpx
            flex
            justify-center
            items-center
            text-24rpx
            @click="join(item)">
            加入
          </view>
          <view
            v-else
            bg="#D9D9D9"
            w-100rpx
            h-50rpx
            rounded-20rpx
            flex
            justify-center
            items-center
            text-24rpx
            text="#7D7D7D">
            已加入
          </view>
        </view>
      </view>
    </view>
    <template #empty>
      <view w-710rpx flex flex-col justify-center items-center>
        <image w-670rpx h-264rpx src="@/pages/company/static/search_empty.png"></image>
        <view text="#7D7D7D" text-28rpx mt-40rpx w-500rpx text-center>
          对不起，没有找到您需要的{{ typeObj[type] }}名称，您可以点击下方按钮手动添加
        </view>
      </view>
    </template>
    <template v-if="dataList.length === 0" #bottom>
      <view w-full px-40rpx pb-46rpx pt-20rpx box-border>
        <view
          w-670rpx
          h-100rpx
          rounded-50rpx
          bg="#FCC300"
          flex
          justify-center
          items-center
          font-500
          text="#000"
          text-32rpx
          @click="toAdd">
          去添加
        </view>
      </view>
    </template>
  </z-paging>
</template>

<script setup lang="ts">
import { selectUnitList, joinUnit } from '@/api/myUnit'
import { selectSchoolList, joinSchool } from '@/api/mySchool'
import { onLoad } from '@dcloudio/uni-app'

const paging = ref<any>(null)
const typeObj = ref<any>({
  1: '单位',
  2: '学校'
})
const keyword = ref<string>('')
const type = ref<number>(1)
const dataList = ref<any>([])
function toAdd() {
  uni.redirectTo({
    url: `/pages/company/${type.value === 1 ? 'addCompany' : 'addSchool'}`
  })
}
function handleQuery() {
  paging.value.reload()
}
const queryList = (pageNo: number, pageSize: number) => {
  const api = type.value === 1 ? selectUnitList : selectSchoolList
  api({
    [type.value === 1 ? 'unitName' : 'schoolName']: keyword.value,
    pageNumber: pageNo,
    pageSize
  })
    .then((res: any) => paging.value.complete(res.data.result.records))
    .catch(() => paging.value.complete(false))
}
function join(item: any) {
  const joinApi = type.value === 1 ? joinUnit : joinSchool
  joinApi(item.id).then(() => {
    uni.showToast({
      title: '加入成功',
      icon: 'success',
      mask: true
    })
    goBack()
  })
}
function goBack() {
  setTimeout(() => {
    uni.navigateBack({
      delta: 1 // 回退前 delta(默认为1) 页面数
    })
  }, 2000)
}
onMounted(() => {})
onLoad((options: any) => {
  type.value = parseInt(options.type)
  uni.setNavigationBarTitle({
    title: typeObj.value[type.value] + '列表'
  })
})
</script>
<style>
page {
}
</style>
<style scoped lang="scss"></style>
